package playnsimple.core.state;

import static playn.core.PlayN.graphics;
import static playn.core.PlayN.pointer;

import com.google.common.collect.Lists;


import playn.core.CanvasImage;
import playn.core.Font;
import playn.core.ImageLayer;
import playn.core.Layer;
import playn.core.PlayN;
import playn.core.Pointer;
import playn.core.Touch;
import playn.core.Touch.Event;
import playnsimple.core.PlaynSimple;
import playnsimple.core.graphics.ImageStack;
import playnsimple.core.graphics.TextImage;
import pythagoras.f.Point;


public class HomeState extends BaseState {
    
    /* 
     * Display title info. On click/tap, go to next to next state.
     */
    private CanvasImage titleImage;
    private CanvasImage subtitleImage;
    private CanvasImage footerImage;
    private ImageStack titleStack;
    private ImageLayer titleImageLayer;

    public HomeState(String label) {
        super(label);
    }
    
    @Override
    public void enter() {
        titleImageLayer = buildTitleLayer();
        addListeners();
    }

    @Override
    public void update() {

    }
    
    @Override
    public void paint() {   
        graphics().rootLayer().add(titleImageLayer);
    }
    
    /*
     * Additional Methods
     */
    private void addListeners() {         
        titleImageLayer.addListener(new Pointer.Listener() {
            @Override
            public void onPointerStart(Pointer.Event event) {
                PlaynSimple.log("Pointer start: " + event);
                PlayN.log().info("Is touch: " + event.isTouch());
            }
            
            @Override
            public void onPointerEnd(Pointer.Event event) {
                PlaynSimple.log("Pointer end: " + event);
                PlaynSimple.log("a hit!");
                machine.changeState("play");
            }

            @Override
            public void onPointerDrag(Pointer.Event event) {
                PlaynSimple.log("Pointer drag: " + event);
            }
        });               
    }
    
    private ImageLayer buildTitleLayer() {
        TextImage title = new TextImage("PlaynSimple");
        title.fontSize = 80;
        title.fontStyle = Font.Style.BOLD;
        titleImage = title.getImage();
        
        TextImage subtitle = new TextImage("a simple PlayN prototype");
        subtitle.fontSize = 20;
        subtitleImage = subtitle.getImage();
        
        TextImage footer = new TextImage("tap here to continue");
        footer.fontSize = 16;
        footerImage = footer.getImage();
        
        titleStack = new ImageStack(titleImage);
        titleStack.addToBottom(subtitleImage, 4);
        titleStack.addToBottom(footerImage, 60); 
        
        ImageLayer titleImageLayer = titleStack.layer();
        float x = (graphics().width() - titleImageLayer.width()) * .5f;
        float y = (graphics().height() - titleImageLayer.height()) * .5f;
        titleImageLayer.setTranslation(x, y);
        
        return titleImageLayer;
    }

}
